﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Windows;
using VRacer.Packets;
using VRacer.Connection;

namespace VRacer
{
    /// <summary>
    /// Odzwierciedla ruchy zawodnika znajdujacego sie na serwerze. 
    /// Zawiera takze polaczenia tcp do gracza i umozliwia wysylanie i odbieranie pakietow.
    /// Implementuje IPacketReceiver i IPacketSender, czyli pozwala nasluchiwac czy wyslano lub odebrano pakiet.
    /// </summary>
    public class ServerPlayer : PlayerBase, IPacketReceiver, IPacketSender
    {
        public event EventHandler Disconnected;

        protected ClientConnection _connection;
        private int _version;
        private bool _ready = false;

        public ServerPlayer(TcpClient connection)
        {
            Connection = new ClientConnection(connection);
            Connection.KeepAlive = false;
            Connection.PacketReceived += new PacketReceivedHandler(Connection_PacketReceived);
            Connection.PacketSent += new PacketSentHandler(Connection_PacketSent);

            Connection.Disconnected += (object sender, EventArgs e) =>
            {
                if (Disconnected != null)
                    Disconnected(this, e);
            };
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ServerPlayer"/> is ready.
        /// </summary>
        /// <value><c>true</c> if ready; otherwise, <c>false</c>.</value>
        public bool Ready
        {
            get { return _ready; }
            set { _ready = value; }
        }

        /// <summary>
        /// Gets or sets the client version.
        /// </summary>
        /// <value>The version.</value>
        public int Version
        {
            get { return _version; }
            set { _version = value; }
        }

        void Connection_PacketSent(object sender, PacketBase packet)
        {
            if (PacketSent != null)
                PacketSent(this, packet);
        }

        void Connection_PacketReceived(object sender, PacketBase packet)
        {
            if (PacketReceived != null)
                PacketReceived(this, packet);
        }

        protected ClientConnection Connection
        {
            get { return _connection; }
            set { _connection = value; }
        }

        public override void Crash(System.Windows.Point crashPoint)
        {
            base.Crash(crashPoint);
        }

        public override void Crash(System.Windows.Point crashPoint, System.Windows.Point startPoint)
        {
            base.Crash(crashPoint, startPoint);
        }

        public override void EndTurn()
        {
            base.EndTurn();
        }

        public override void Hit(PlayerBase player)
        {
            base.Hit(player);
        }

        public override void Move(System.Windows.Point point)
        {
            base.Move(point);
        }

        public override void StartTurn()
        {
            base.StartTurn();
        }

        #region IPacketReceiver Members

        public event PacketReceivedHandler PacketReceived;

        #endregion

        #region IPacketSender Members

        public event PacketSentHandler PacketSent;

        public void Send(PacketBase packet)
        {
            Connection.Send(packet);
        }

        #endregion
    }
}
